//
//  NSObject+Tip.m
//  jhjr
//
//  Created by winter on 15/12/29.
//  Copyright © 2015年 DJDG. All rights reserved.
//

#import "NSObject+Tip.h"
#import "MBProgressHUD.h"

@implementation NSObject (Tip)

#pragma mark - Tip

+ (NSString *)tipFromError:(NSError *)error
{
    if (error && error.userInfo) {
        NSMutableString *tipStr = [NSMutableString string];
        if ([error.userInfo objectForKey:@"msg"]) {
            id msg = [error.userInfo objectForKey:@"msg"];
            if ([msg isKindOfClass:[NSDictionary class]]) {
                NSArray *msgArray = [(NSDictionary *)msg allValues];
                NSUInteger num = msgArray.count;
                for (int i = 0; i < num; i++) {
                    NSString *msgStr = [msgArray objectAtIndex:i];
                    if (i+1 < num) {
                        [tipStr appendString:[NSString stringWithFormat:@"%@\n",msgStr]];
                    }
                    else {
                        [tipStr appendString:msgStr];
                    }
                }
            }
            else if ([msg isKindOfClass:[NSString class]]) {
                tipStr = msg;
            }
        }
        else {
            if ([error.userInfo objectForKey:@"NSLocalizedDescription"]) {
                tipStr = [error.userInfo objectForKey:@"NSLocalizedDescription"];
            }
            else {
                NSError *underlyingError = error.userInfo[@"NSUnderlyingError"];
                if (underlyingError) {
                   return [self tipFromError:underlyingError];
                }
                else [tipStr appendFormat:@"ErrorCode %ld",(long)error.code];
            }
        }
        return tipStr;
    }
    return nil;
}

+ (BOOL)showError:(NSError *)error
{
    NSString *tipStr = [NSObject tipFromError:error];
    [NSObject showHudTipStr:tipStr delay:YES];
    return YES;
}

+ (void)showHudTipStr:(NSString *)tipStr
{
    [self showHudTipStr:tipStr delay:NO];
}

+ (void)showHudTipStr:(NSString *)tipStr delay:(BOOL)delay
{
    if (tipStr && tipStr.length > 0) {
        
        NSTimeInterval afterDelay = [self calculateDelayTimeWith:tipStr];
        
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:kKeyWindow animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.detailsLabel.font = [UIFont boldSystemFontOfSize:15.0];
        hud.detailsLabel.text = tipStr;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hideAnimated:YES afterDelay:afterDelay];
    }
}

+ (CGFloat)calculateDelayTimeWith:(NSString *)text
{
    CGFloat delay = 1.5;
    delay += text.length / 20;
    return delay;
}

#pragma mark - NetError

- (id)handleResponse:(id)responseJSON
{
    return [self handleResponse:responseJSON autoShowError:YES];
}

- (id)handleResponse:(id)responseJSON autoShowError:(BOOL)autouShowError
{
    NSError *error = nil;
    
    NSNumber *resultCode = [responseJSON valueForKey:@"code"];
    
    // 非0
    if (resultCode.integerValue != 0) {
        error = [NSError errorWithDomain:@"出错了" code:resultCode.integerValue userInfo:responseJSON];
        if (autouShowError) {
            [NSObject showError:error];
        }
    }
    return error;
}
@end
